//
//  YTWalletAddrController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/9/12.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTWalletAddrController.h"
#import "DHQRCodeController.h"

@interface YTWalletAddrController ()
@property (weak, nonatomic) IBOutlet UITextField *walletAddrTxtField;
@property (weak, nonatomic) IBOutlet UITextField *walletNameLabel;
@property (weak, nonatomic) IBOutlet UIButton *changeCoinBtn;
@property(nonatomic ,strong)NSArray *productList;

@property(nonatomic ,strong)id selectCoinType;
@end

@implementation YTWalletAddrController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNav];
    
    [self loadCoinDataList];
    
    if (self.wallet_id) {
        [self loadEditData];
    }
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"YTWalletAddrController" bundle:nil];
        YTWalletAddrController *vc = [sb instantiateViewControllerWithIdentifier:@"addWalletAddrID"];
        self = vc;
    }
    return self;
}

- (void)loadEditData
{
    [YTNetworkManager.shareManager requestGetWalletInfoWithID:self.wallet_id
                                                   completion:^(id json, BOOL isSucc, NSString *error)
    {
        if (!isSucc) {
            [self.tableView showErrorWithTipStr:error];
            return ;
        }
        self.walletAddrTxtField.text = json[@"addr"];
        self.walletNameLabel.text = json[@"name"];
        self.selectCoinType = @{@"id":json[@"product_id"]};
        [self.changeCoinBtn setTitle:json[@"product_name"] forState:UIControlStateNormal];
        self.changeCoinBtn.selected = YES;

        NSLog(@"%@",json);
    }];
}
- (void)loadCoinDataList
{
    [YTNetworkManager.shareManager requestGetProductListWithType:@"1"
                                                      completion:^(NSArray *list, BOOL isSucc, NSString *error)
    {
        if (!isSucc) {
            [self.tableView showErrorWithTipStr:error];
            return ;
        }
        
        self.productList = list;
    }];
}

#pragma mark: - 二维码扫描 监听
- (IBAction)rcodeButtonClick:(UIButton *)sender
{
    __weak typeof(self) WeakSelf = self;
    DHQRCodeController *vc = [DHQRCodeController qrCodeControllerWithResultBlock:^(LBXScanResult *result, NSString *error) {
        
        if (result) {
            WeakSelf.walletAddrTxtField.text = result.strScanned;
        }
    }];
    [self.navigationController pushViewController:vc animated:true];
}

#pragma mark: - 兑换按钮 监听
- (IBAction)exchangButtonClick:(UIButton *)sender
{
    [self.tableView endEditing:YES];
    [MOFSPickerManager.shareManger showPickerViewWithDataArray:[self.productList valueForKey:@"name"]
                                                           tag:1 title:nil
                                                   cancelTitle:@"取消"
                                                   commitTitle:@"确定"
                                                   commitBlock:^(NSString * _Nonnull string)
     {
         
         
    } cancelBlock:nil];
    [MOFSPickerManager.shareManger showPickerViewWithCustomDataArray:self.productList
                                                           keyMapper:@"name"
                                                                 tag:1
                                                               title:nil
                                                         cancelTitle:@"取消"
                                                         commitTitle:@"确定"
                                                         commitBlock:^(id model)
    {
        [sender setTitle:model[@"name"] forState:UIControlStateSelected];
        sender.selected = YES;
        self.selectCoinType = model;
    } cancelBlock:nil];
}

#pragma mark: - setupNav
- (void)setupNav
{
    [self.navigationController.navigationBar setTintColor:UIColor.blackColor];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"钱包地址";
    titleLabel.font = [UIFont boldSystemFontOfSize:16];
    titleLabel.textColor = UIColor.blackColor;
    self.navigationItem.titleView = titleLabel;
    
}
#pragma mark: - 清空按钮 监听 112233--地址txt上的 112234--钱包名txt上的
- (IBAction)clearButtonClick:(UIButton *)sender
{
    (sender.tag == 112233) ? [self.walletAddrTxtField setText:@""] : [self.walletNameLabel setText:@""];
}

#pragma mark: - 确认按钮 监听
- (IBAction)confirmButtonClick:(UIButton *)sender
{
    if (!self.walletNameLabel.hasText ||
        !self.walletAddrTxtField.hasText ||
        !self.changeCoinBtn.selected) {
        [self.tableView showErrorWithTipStr:@"请完善信息"];
        return;
    }
    
    [YTNetworkManager.shareManager requestAddOrEditeWalletAddrWithName:self.walletNameLabel.text
                                                                  addr:self.walletAddrTxtField.text
                                                            product_id:self.selectCoinType[@"id"]
                                                             wallet_id:self.wallet_id
                                                            completion:^(BOOL isSucc, NSString *error)
    {
        if (!isSucc) {
            [self.tableView showErrorWithTipStr:error];
            return ;
        }
        
        [self.tableView showTipWithTipStr:self.wallet_id ? @"更新成功" : @"添加成功"];
        [self.navigationController popViewControllerAnimated:true];
    }];
}




@end
